<template>
	<view class="tree-revision">
		<view @click.stop="handleRecursion(item,index)" v-for="(item,index) in children" :key="item.id">
			<view
				:style="{'display':'flex','align-items': 'center', 'justify-content': 'space-between','padding-left':30*item.codeLevel-1 + 'rpx','padding-right':'50rpx'}"
				class="checkout-device">
				<text>{{ item.name }}</text>
				<image v-if="typeData=='space' && !item.lastLevelFlag || typeData=='business' && !item.groupType"
					:class="{'spin' :item.spread }"
					src="https://fireware-1257276602.cos.ap-guangzhou.myqcloud.com/fsw-wx-lmini/icons/icons/Vector_stroke.png">
				</image>
				<text @click="handleCheckoutDevice(item)"
					v-if="typeData=='space' && item.lastLevelFlag || typeData=='business' && item.groupType"
					class="checkout-device-item">选择</text>
			</view>
			<treeRevision :typeData="typeData" :class="{ 'conceal' : !item.spread }"
				v-if="item.children && item.children.length" :children="item.children"></treeRevision>
		</view>
	</view>
</template>

<script>
	import {
		request
	} from "../../../request/index.js";
	import treeRevision from "./tree-revision.vue"

	export default {
		components: {
			treeRevision
		},
		props: {
			children: {
				type: Array
			},
			typeData: {
				type: String
			},
			likeCode: {
				type: String
			},
			codeLevel: {
				type: String
			}
		},
		data() {
			return {

			};
		},
		mounted() {},
		methods: {
			async getSpaceClass({
				likeCode,
				codeLevel
			}) {
				let res = await request({
					url: "api/oct/wemini/bus/workOrder/findSpaceList",
					method: "POST",
					data: {
						likeCode,
						codeLevel,
						projectId: uni.getStorageSync("projectId") || "16154516457903604960000255341083"
					}
				})
				if (res.status == 0) {
					res.payload.forEach(item => item.spread = false)
					return res
				} else {
					uni.showToast({
						title: '获取失败!',
						icon: 'none',
						duration: 1000,
						success: (res) => {

						},
					})
				}

			},
			async handleRecursion(item, index) {
				console.log('item', item);
				let obj = this.children[index]
				console.log(obj.spread);
				if (obj.spread && obj.children && obj.children.length) {
					//  如果spread为真,代表箭头朝下(有children)
					this.$set(obj, 'spread', false)
					this.$forceUpdate()
					return
				} else if (!obj.spread && obj.children && obj.children.length) {
					//  如果spread为假,有children
					this.$set(obj, 'spread', true)
					this.$forceUpdate()
					return
				} else {
					//  没有数据的情况,拉去下一级数据
					if (this.typeData == "space") {
						//  最后一级不做任何处理
						if (obj.lastLevelFlag) return
						//  获取空间下一级数据
						let res = await this.getSpaceClass({
							likeCode: item.code,
							codeLevel: item.codeLevel + 1
						})
						if (res.status == 0) {
							let list = res.payload
							list.forEach(item => {
								item.joint = obj.joint + '-' + item.name
							})

							this.$set(obj, 'spread', true)
							this.$set(obj, 'children', list)
							this.$forceUpdate()
							return
						} else {
							uni.showToast({
								title: '获取失败!',
								icon: 'none',
								duration: 1000,
								success: (res) => {

								},
							})
						}

					} else if (this.typeData == "business") {
						//  获取业务下一级数据
						let res = await request({
							url: "api/oct/wemini/bus/workOrder/findDataCategoryList",
							method: "POST",
							data: {
								categoryCodeLevel: item.codeLevel + 1,
								likeCategoryCode: item.code,
							}
						})
						if (res.status == 0) {
							let list = res.payload
							// list.forEach(item => {
							// 	item.joint = item.joint + '-' + item.name
							// })

							this.$set(obj, 'spread', true)
							this.$set(obj, 'children', list)
							this.$forceUpdate()
							console.log('obj', obj);
						} else {
							uni.showToast({
								title: "获取失败!",
								icon: 'none',
								duration: 1000,
								success: (res) => {},
							})
						}
					}

				}
			},
			handleCheckoutDevice(item) {
				let routes = getCurrentPages();
				let curRoute = routes[routes.length - 2]
				if (this.typeData == "space") {
					curRoute.$vm.workOrderObjects.push({
						...item,
						objectType: 2
					})
				} else if (this.typeData == "business") {
					this.$set(curRoute.$vm.form, 'baseFlagName', item.flagName)
					this.$set(curRoute.$vm.form, 'flagName', item.name)
				}
				uni.navigateBack({
					delta: 1,
				})
			}
		}
	}
</script>

<style lang="less">
	/* components/tree-revision.wxss */
	.checkout-device {
		text-align: left;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #f0f0f0;

		image {
			width: 16rpx;
			height: 28rpx;
		}
	}



	.spin {
		transform: rotate(90deg);
	}

	.conceal {
		display: none !important;
		/* height: 0rpx !important; */
	}

	.checkout-device-item {
		width: 120rpx;
		height: 40rpx;
		background: #6390FD;
		border-radius: 18rpx;
		line-height: 40rpx;
		font-size: 24rpx;
		color: #fff;
		text-align: center;
	}
</style>